package com.example.zhou.zhihunews;


import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.load.model.GlideUrl;
import com.example.zhou.base.BaseActivity;
import com.example.zhou.callback.LoadingPageCallback;
import com.example.zhou.connect.LoadingPageConnect;
import com.example.zhou.heplper.MyTimer;

import org.json.JSONException;
import org.json.JSONObject;


import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends BaseActivity implements LoadingPageCallback {
    @BindView(R.id.iv_page)
    ImageView iv_load_page;
    @BindView(R.id.tv_name)
    TextView tv_name;
    private LoadingPageConnect connect = new LoadingPageConnect();
    private MyTimer myTimer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        connect.onLoadingPage(this);

        myTimer = new MyTimer(4000, 1000, this, NewsActivity.class);
    }

    @Override
    public void onLoadingPageSuccess(String loadingPageSucceed) {
        Log.e("loginPageSuccessed", loadingPageSucceed);
        try {
            JSONObject jsonObject = new JSONObject(loadingPageSucceed);
            final String text = jsonObject.optString("text");
            final String img = jsonObject.optString("img");
            //  tv_name.setText(text);

            myTimer.start();
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    requestBuilder.load(new GlideUrl(img)).into(iv_load_page);
                    tv_name.setText(text);
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onLoadingPageFail(String loadingPageFailed) {
        Looper.prepare();
        Toast.makeText(this, "失败！", Toast.LENGTH_LONG).show();
    }


}
